package com.cn.demo4.dao.impl;

import com.cn.demo4.dao.NavDao;
import com.cn.demo4.model.NavModel;
import com.cn.demo4.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NavDaoImpl implements NavDao {
    @Override
    public List<NavModel> findAll(NavModel navModel) {
//        String sql = "select n.*,t.name type_name from nav n,nav_type t where n.type_id=t.id order by id desc";
        //1.sql查询语句
        StringBuilder sql = new StringBuilder();
        sql.append("select n.*,t.name type_name from nav n,nav_type t where true and n.type_id=t.id ");
        if (!"".equals(navModel.getName())) {
            sql.append(" and n.name like '%").append(navModel.getName()).append("%'");
        }
        if (!"全部".equals(navModel.getTypeName()) && navModel.getTypeName() != null) {
            sql.append(" and t.name like '%").append(navModel.getTypeName()).append("%'");
        }
        if (!"".equals(navModel.getHref())) {
            sql.append(" and n.href like '%").append(navModel.getHref()).append("%'");
        }
        if (navModel.getStatus() != -1) {
            if (navModel.getStatus() == 1) {
                sql.append(" and n.status=1");
            } else {
                sql.append(" and n.status=0 or n.status is null ");
            }
        }
        sql.append(" order by n.id desc");
        sql.append(" limit ").append((navModel.getPageModel().getCurPage() - 1) * navModel.getPageModel().getPageSize()).append(",").append(navModel.getPageModel().getPageSize());
        ResultSet resultSet = JDBC.select(sql.toString());
        return getData(resultSet);
    }

    @Override
    public int add(NavModel navModel) {

        String sql = "insert into nav(name,href,status,img,type_id,content,create_time,update_time)" +
                " values('" + navModel.getName() + "'," +
                "'" + navModel.getHref() + "'," +
                "" + navModel.getStatus() + "," +
                "'" + navModel.getImg() + "'," +
                "" + navModel.getTypeId() + "," +
                "'" + navModel.getContent() + "'," +
                " now(),now())";
        return JDBC.upDate(sql);
    }

    @Override
    public NavModel findById(Integer id) {
        String sql = "select n.*,t.name type_name from nav n,nav_type t where n.type_id=t.id and n.id=" + id;
        ResultSet resultSet = JDBC.select(sql);
        List<NavModel> list = getData(resultSet);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public int update(NavModel navModel) {
        String sql = "update nav set name='" + navModel.getName() + "'," +
                " href='" + navModel.getHref() + "'," +
                " status=" + navModel.getStatus() + ",";
        if (navModel.getImg() != null && !navModel.getImg().equals("undefined")) {
            sql += " img='" + navModel.getImg() + "',";
        }
        sql += " type_id=" + navModel.getTypeId() + "," +
                " content='" + navModel.getContent() + "'," +
                " update_time=now()" +
                " where id=" + navModel.getId();
        return JDBC.upDate(sql);
    }

    @Override
    public int del(Integer id) {
        String sql = "delete from nav where id=" + id;
        return JDBC.upDate(sql);
    }


    /**
     * 公共方法：得到数据库数据
     *
     * @param resultSet
     * @return
     */
    private List<NavModel> getData(ResultSet resultSet) {
        List<NavModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                NavModel navModel = new NavModel();
                navModel.setId(resultSet.getInt("id"));
                navModel.setName(resultSet.getString("name"));
                navModel.setHref(resultSet.getString("href"));
                navModel.setStatus(resultSet.getInt("status"));
                navModel.setCreateTime(resultSet.getString("create_time"));
                navModel.setUpdateTime(resultSet.getString("update_time"));
                navModel.setImg(resultSet.getString("img"));
                navModel.setTypeId(resultSet.getInt("type_id"));
                navModel.setTypeName(resultSet.getString("type_name"));
                navModel.setContent(resultSet.getString("content"));
                list.add(navModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 前台导航数据
     *
     * @return
     */
    @Override
    public List<NavModel> getNavModelListForHome() {
        String sql = "select n.*,t.name type_name from nav n,nav_type t where n.type_id=t.id and n.status=1";
        ResultSet resultSet = JDBC.select(sql);
        return getData(resultSet);
    }

    @Override
    public int getCount(NavModel navModel) {
        StringBuilder sql = new StringBuilder();
        sql.append("select count(*) count  from nav n,nav_type t  where true and n.type_id=t.id ");
        if (!"".equals(navModel.getName())) {
            sql.append(" and n.name like '%").append(navModel.getName()).append("%'");
        }
        if (!"全部".equals(navModel.getTypeName()) && navModel.getTypeName() != null) {
            sql.append(" and t.name like '%").append(navModel.getTypeName()).append("%'");
        }
        if (!"".equals(navModel.getHref())) {
            sql.append(" and n.href like '%").append(navModel.getHref()).append("%'");
        }
        if (navModel.getStatus() != -1) {
            if (navModel.getStatus() == 1) {
                sql.append(" and n.status=1");
            } else {
                sql.append(" and n.status=0 or n.status is null ");
            }
        }
        sql.append(" order by n.id desc");
        ResultSet resultSet = JDBC.select(sql.toString());
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}

